package com.mylib.libcore.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.mylib.libcore.R;
import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.tools.TTAdManagerHolder;
import com.mylib.libcore.utils.ScreenUtils;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.widget.CustomCoinToast;

import static com.mylib.libcore.config.AppConfig.CSJ_BANNER_ONE_ID;


/**
 * 通用金币奖励弹框
 */
public class CommonGoldDialog extends PopupWindow {

    private TextView tv_coin_title,tv_cancel;
    private View mMenuView;
    public DialogViewClink dialogViewClink;
    private Activity mActivity;
    //看视频
    private TTRewardVideoAd mRewardVideoAd;
    private TTAdNative mTTAdNative;
    private boolean videoPrepare = false;
    public interface  DialogViewClink{
        void viewClink(boolean isSign);
    }

    public CommonGoldDialog(int width, String coinTitle,String goldcoin, Activity context) {
        super(context);
        this.mActivity =context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_common_gold_style_view, null);
        tv_coin_title = (TextView) mMenuView.findViewById(R.id.tv_coin_title);
        tv_cancel = (TextView) mMenuView.findViewById(R.id.tv_cancel);
        if(!UIUtils.isEmpty(coinTitle))tv_coin_title.setText(coinTitle+"");
        TextView tv_lottery_name = (TextView) mMenuView.findViewById(R.id.tv_coin_num);
        TextView tv_btn = (TextView) mMenuView.findViewById(R.id.tv_btn);
        tv_lottery_name.setText(""+goldcoin);
        RelativeLayout rl_ad = (RelativeLayout)mMenuView.findViewById(R.id.rl_ad);
        CSJADTools.loadCSJAdMiddleBanner(context,rl_ad,"",CSJ_BANNER_ONE_ID,width,90);


        ImageView iv_close = (ImageView) mMenuView.findViewById(R.id.iv_close);
        tv_btn.setOnClickListener(v -> prepareVideo());
        iv_close.setOnClickListener(v -> dismiss());
        tv_cancel.setOnClickListener(v -> dismiss());
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setClippingEnabled(false);
        }


        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ScreenUtils.getScreenWidth(context));
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ScreenUtils.getScreenHeight(context)+ ScreenUtils.getScreenHeight(context)+200);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
//        //设置SelectPicPopupWindow弹出窗体动画效果-默认中间
       // this.setClippingEnabled(false);
//        this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(mContext.getResources().getColor(R.color.transparent));
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框

    }



    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

    //初始化穿山甲广告
    private void initCSJAD( ){
        try{
            TTAdManagerHolder.init(mActivity);
            mTTAdNative = TTAdManagerHolder.getTTAdNative(mActivity);
            mTTAdNative.loadRewardVideoAd(CSJADTools.getAdSlot("任务奖励",3,"task"),
                    new TTAdNative.RewardVideoAdListener() {
                        @Override
                        public void onError(int i, String s) {
                            videoPrepare = false;
                        }

                        @Override
                        public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                            videoPrepare = false;
                            if(UIUtils.isEmpty(ttRewardVideoAd)) return;
                            mRewardVideoAd = ttRewardVideoAd;
                        }
                        @Override
                        public void onRewardVideoCached() {
                            videoPrepare = true;
                        }
                    });

        }catch (Exception e){}
    }

    //准备视频
    private void prepareVideo(){
        if (mRewardVideoAd != null && videoPrepare) {
            dismiss();
            mRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
                @Override
                public void onAdShow() { }

                @Override
                public void onAdVideoBarClick() { }

                @Override
                public void onAdClose() {
                    CustomCoinToast.showToast(mActivity,"额外奖励","3金币");
                }

                @Override
                public void onVideoComplete() {}

                @Override
                public void onVideoError() {}

                @Override
                public void onRewardVerify(boolean b, int i, String s, int i1, String s1) { }

                @Override
                public void onSkippedVideo() { }
            });
            //展示广告，并传入广告展示的场景
            mRewardVideoAd.showRewardVideoAd(mActivity, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
        } else{
            ToastUtil.show("视频资源准备中，请稍后再试");
            initCSJAD();
        }
    }

}